// src/store/modules/user/index.js

import { getCfg } from '@/apis/account';
import { get_uinfo } from '@/apis/user';
import { setUser } from '@/utils/auth';
import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      userInfo: {},
      sn: '',
      token: '',
      sysCfg: {}
    };
  },
  actions: {
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo;
    },
    setToken(token: string) {
      this.token = token;
    },
    setSn(sn :string) {
      this.sn = sn;
    },
    async getUserData() {
      try {
        const { data, ok } = await get_uinfo();
      if (ok) {
        this.setUserInfo(data);
        setUser(data);
      }
      } catch (error) {
        console.log('getUserData:', error);
      }
    },
    getCfgData() {
      getCfg().then((res: any) => {
        if (res.ok) {
          this.sysCfg = res.data;
        }
      })
    }
  },
});
